package com.block.datapush.service.apipush;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.BeiJingJiUtil;
import com.block.datapush.service.apipush.utils.JiSuDaiUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 北京吉识宇信
 */
@Service
public class BeijingJiShiApiService implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();

    @Autowired
    IMemberCommonService memberCommonService;


    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);


        String queryUrl = pushApiKeyParam.getCheckUrl();


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("phone", memberSourceInfo.getMobileMd5());



        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("ticket",  pushApiKeyParam.getApiSecret());

        log.info("【北京吉识宇信】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createGet(queryUrl)
                .headerMap(headerMap,true)
                .form(paramMap)
                .execute().body();
        log.info("【北京吉识宇信】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        log.info("撞库结果resultObject : {}", resultObject);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");


        pushApiResult.setCode(code + "|");
        pushApiResult.setMessage(msg);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "200")) {
            pushApiResult.setPushSuccess(true);
        }
        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        return null;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);

        String requestUrl = pushApiKeyParam.getRegisterUrl();

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("accumulation", BeiJingJiUtil.getGjj(memberSourceInfo.getGjj()));
        bizMap.put("age", memberSourceInfo.getAge());
        bizMap.put("annualIncome", "");
        bizMap.put("business", 0);
        bizMap.put("car", BeiJingJiUtil.getCar(memberSourceInfo.getCar()));
        bizMap.put("city", memberSourceInfo.getCity());
        bizMap.put("enterprise", "");
        bizMap.put("issued", 0);
        bizMap.put("loanLimit", BeiJingJiUtil.getloan(memberSourceInfo.getLoanAmount()));
        bizMap.put("marriageStatus", "");
        bizMap.put("mobile", memberSourceInfo.getMobile());
        bizMap.put("mobileMd5", memberSourceInfo.getMobileMd5());
        bizMap.put("name", memberSourceInfo.getRealname());
        bizMap.put("policy", BeiJingJiUtil.getbaodan(memberSourceInfo.getBaoxian()));
        bizMap.put("room", BeiJingJiUtil.getHouse(memberSourceInfo.getHouse()));
        bizMap.put("sex", memberSourceInfo.getSex() == 1 ? 0 : 1);
        bizMap.put("social", BeiJingJiUtil.getshebao(memberSourceInfo.getShebao()));
        bizMap.put("remark","客户芝麻分："+ BaseTypeInfoEnum.Zhima.getNameByValue(memberSourceInfo.getZhima())+"   |   "
                +"客户社保：" + BaseTypeInfoEnum.Shebao.getNameByValue(memberSourceInfo.getShebao())
                +"客户白条："+BaseTypeInfoEnum.Baitiao.getNameByValue(memberSourceInfo.getBaitiao())
                +"客户逾期情况"+BaseTypeInfoEnum.Yuqi.getNameByValue(memberSourceInfo.getYuqi()));


        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("ticket",  pushApiKeyParam.getApiSecret());

        log.info("【北京吉识宇信】导单开始【queryUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String result = HttpUtil.createPost(requestUrl)
                .headerMap(headerMap,true)
                .body(JSON.toJSONString(bizMap))
                .execute().body();
        log.info("【北京吉识宇信】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");


        pushApiResult.setCode(code + "|");
        pushApiResult.setMessage(msg);
        pushApiResult.setParamInfo(JSON.toJSONString(bizMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "200")) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }
        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());
        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        return pushApiResult;
    }


    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();
        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }

    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        ProtocolParamResult paramResult = new ProtocolParamResult();
        return paramResult;
    }
}
